package com.yanqu.road.entity.activity.tonbing.data;


import com.yanqu.road.entity.activity.tonbing.CrossTonBingRankCampIntSort;
import com.yanqu.road.entity.activity.tonbing.CrossTonBingRankSort;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.Objects;

public class CrossTonBingRankListModel {

    private Comparator<CrossTonBingRank> rankSort = new CrossTonBingRankSort();

    private Comparator<CrossTonBingRank> rankCampSort = new CrossTonBingRankCampIntSort();

    private LinkedList<CrossTonBingRank> rankList = new LinkedList<>();

    public void rankChange(CrossTonBingRank rank) {
        synchronized (this) {

            boolean change = false;

            boolean find = false;
            for (CrossTonBingRank tmpRank : rankList) {
                // 找到了
                if (Objects.equals(tmpRank.getId(), rank.getId())) {
                    tmpRank.setValue(rank.getValue());
                    tmpRank.setTime(rank.getTime());
                    change = true;
                    find = true;
                    break;
                }
            }

            if (!find) {
                rankList.add(rank);
                change = true;
            }

            if (change) {
                rankSort();
            }

        }
    }

    public LinkedList<CrossTonBingRank> getRankList() {
        return new LinkedList<>(rankList);
    }

    public void setRankList(LinkedList<CrossTonBingRank> rankList) {
        synchronized (this) {
            this.rankList = rankList;
            rankSort();
        }
    }

    //这个排行每次都是请求了再生成的，不用管更新，才能这样
    public void setRankCampList(LinkedList<CrossTonBingRank> rankList) {
        synchronized (this) {
            this.rankList = rankList;
            rankCampSort();
        }
    }

    /**
     * 排序，记得加锁
     */
    private void rankSort() {
        rankList.sort(rankSort);
    }
    private void rankCampSort() {
        rankList.sort(rankCampSort);
    }
}
